#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 
# # 爬取图片到本地

#导入requests库
import requests     

def getImage(url):
    #异常处理
    try:
        r = requests.get(url)
        r.raise_for_status()                # 如果状态码不是200，抛出异常
        # 将图片保存到本地
        f = open('test.jpg','wb')
        f.write(r.content)
        f.close
        return 'test.jpg 保存成功'                    # 返回内容的二进制形式
    except:
        return "发生异常！"

# 爬取图片
image_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1547034974259&di=774186c3e29960e099294d4516cde48e&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F58ee3d6d55fbb2fbbc6b4796424a20a44723dcf6.jpg'
print(getImage(image_url))